﻿using DemountCIM.Framework.SECSGEM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemountCIM.Framework.PLC
{
    public enum PlcDef
    {
        PlateCount = 6,

        // 设备状态
        Error = 0,
        InProcess,
        Standby
    }

    public class PlcData
    {
        #region // 字段

        public object dataLock;                 // 数据锁
        /// <summary>
        /// 0:晶托到达脱胶机工位
        /// 1:晶托到达RFID读取工位
        /// 2:晶托离开脱胶机工位
        /// </summary>
        public bool[] demounter1Station;         // 脱胶机1工位
        /// <summary>
        /// 0:晶托到达脱胶机工位
        /// 1:晶托到达RFID读取工位
        /// 2:晶托离开脱胶机工位
        /// </summary>
        public bool[] demounter2Station;          // 脱胶机2工位
        /// <summary>
        /// 0:晶托到达码垛工位
        /// 1:晶托到达RFID读取工位
        /// 2:晶托离开码垛工位
        /// </summary>
        public bool[] palletizingStation;       // 树脂板码垛工位
        /// <summary>
        /// 0:晶托到达铲胶机工位
        /// 1:晶托离开铲胶机工位
        /// </summary>
        public bool[] removeGlueStation;        // 铲胶机工位
        /// <summary>
        /// 0:晶托到达清洗机工位
        /// 1:晶托离开清洗机工位
        /// 2:晶托到达RFID写入工位
        /// </summary>
        public bool[] cleaningStation;          // 清洗机工位
        public int demounter1State;             // 脱胶机1状态
        public int demounter2State;             // 脱胶机2状态
        public int palletizingState;            // 树脂板码垛机状态
        public int removeGlueState;             // 铲胶机状态
        public int cleaningState;               // 清洗机状态

        public bool[] plateCartLoadASK;             // 晶托上料请求 两个
        public bool[] plateCartUnloadASK;           // 晶托小车下料请求 两个
        public bool[] plateCartArrived;             // 小车到位信号
        public bool[] plateAMRArrived;              // AMR到位信号

        public bool beamCartLoadASK;                // 树脂板上料请求 
        public bool beamCartUnloadASK;              // 树脂板小车下料请求 
        public bool beamCartArrived;                // 小车到位信号
        public bool beamAMRArrived;                 // AMR到位信号

        public int equipmentState;              // 设备状态
        public int equipmentSubState;           // 设备SUB状态
        public int[] curPortAccessMode;         // 当前端口手自动模式

        #endregion


        #region // 方法

        /// <summary>
        /// 构造函数
        /// </summary>
        public PlcData()
        {
            dataLock = new object();


            demounter1Station = new bool[3];
            demounter2Station = new bool[3];
            palletizingStation = new bool[3];
            removeGlueStation = new bool[2];
            cleaningStation = new bool[3];

            plateCartLoadASK = new bool[2];
            plateCartUnloadASK = new bool[2];
            plateCartArrived = new bool[2];
            plateAMRArrived = new bool[2];

            curPortAccessMode = new int[3];
            equipmentState = 0;
            equipmentSubState = 0;

            Release();
        }

        /// <summary>
        /// 复制外部数据到本类
        /// </summary>
        public bool CopyFrom(PlcData pData)
        {
            if (null != pData)
            {
                if (this == pData)
                {
                    return true;
                }

                lock (this.dataLock)
                {
                    lock (pData.dataLock)
                    {
                        for (int nIdx = 0; nIdx < demounter1Station.Length; nIdx++)
                        {
                            demounter1Station[nIdx] = pData.demounter1Station[nIdx];
                        }
                        for (int nIdx = 0; nIdx < demounter2Station.Length; nIdx++)
                        {
                            demounter2Station[nIdx] = pData.demounter2Station[nIdx];
                        }
                        for (int nIdx = 0; nIdx < palletizingStation.Length; nIdx++)
                        {
                            palletizingStation[nIdx] = pData.palletizingStation[nIdx];
                        }
                        for (int nIdx = 0; nIdx < removeGlueStation.Length; nIdx++)
                        {
                            removeGlueStation[nIdx] = pData.removeGlueStation[nIdx];
                        }
                        for (int nIdx = 0; nIdx < cleaningStation.Length; nIdx++)
                        {
                            cleaningStation[nIdx] = pData.cleaningStation[nIdx];
                        }



                        for (int i = 0; i < 2; i++)
                        {
                            plateCartLoadASK[i] = pData.plateCartLoadASK[i];
                            plateCartUnloadASK[i] = pData.plateCartUnloadASK[i];
                            plateCartArrived[i] = pData.plateCartArrived[i];
                            plateAMRArrived[i] = pData.plateAMRArrived[i];
                        }
                        beamCartLoadASK = pData.beamCartLoadASK;
                        beamCartUnloadASK = pData.beamCartUnloadASK;
                        beamCartArrived = pData.beamCartArrived;
                        beamAMRArrived = pData.beamAMRArrived;

                        for (int i = 0; i < curPortAccessMode.Length; i++)
                        {
                            curPortAccessMode[i] = pData.curPortAccessMode[i];
                        }

                        equipmentState = pData.equipmentState;
                        equipmentSubState = pData.equipmentSubState;

                        demounter1State = pData.demounter1State;
                        demounter2State = pData.demounter2State;
                        palletizingState = pData.palletizingState;
                        removeGlueState = pData.removeGlueState;
                        cleaningState = pData.cleaningState;
                    }
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 清除数据
        /// </summary>
        public void Release()
        {
            lock (dataLock)
            {
                for (int nIdx = 0; nIdx < demounter1Station.Length; nIdx++)
                {
                    demounter1Station[nIdx] = false;
                }
                for (int nIdx = 0; nIdx < demounter2Station.Length; nIdx++)
                {
                    demounter2Station[nIdx] = false;
                }
                for (int nIdx = 0; nIdx < palletizingStation.Length; nIdx++)
                {
                    palletizingStation[nIdx] = false;
                }
                for (int nIdx = 0; nIdx < removeGlueStation.Length; nIdx++)
                {
                    removeGlueStation[nIdx] = false;
                }
                for (int nIdx = 0; nIdx < cleaningStation.Length; nIdx++)
                {
                    cleaningStation[nIdx] = false;
                }

                for (int i = 0; i < 2; i++)
                {
                    plateCartLoadASK[i] = false;
                    plateCartUnloadASK[i] = false;
                    plateCartArrived[i] = false;
                    plateAMRArrived[i] = false;
                }
                beamCartLoadASK = false;
                beamCartUnloadASK = false;
                beamCartArrived = false;
                beamAMRArrived = false;

                demounter1State = -1;
                demounter2State = -1;
                palletizingState = -1;
                removeGlueState = -1;
                cleaningState = -1;

            }
        }

        #endregion
    }
}
